<?php


namespace app\admin\controller;

use app\common\model\TouziKLine;
use app\BaseController;
use app\common\model\AdminModel;
use app\common\model\TokenModel;
use app\common\model\PayWithdraw;
use app\common\model\TouziProduct;
use app\common\model\User;
use app\validate\Login as validates;
use think\exception\ValidateException;

class Login extends BaseController
{
    public function initialize()
    {
        //session('admin_user', '');
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function updateKLine(){
        $zhang_die = getSystemConfig('fund_up_or_down');
        $die = getSystemConfig('fund_down_rate');
        $die_arr = explode('#', $die);
        $die_rand = rand($die_arr[0], $die_arr[1]);
        $zhang = getSystemConfig('fund_up_rate'); 
        $zhang_arr = explode('#', $zhang);
        $zhang_rand = rand($zhang_arr[0], $zhang_arr[1]);
        $price_open = 0;
        $price_close = 0;
        $rand = 1;
        if($zhang_die == '0'){
            // 跌
            $rand = $die_rand * -1;
        }
        if($zhang_die == '1'){
            // 涨
            $rand = $zhang_rand;
        }
        // 获取昨日价格 
        $last = (new TouziKLine())->whereBetweenTime('show_day', 0, date('Y-m-d',(time()- 24 * 60 *60)))->order('id desc')->limit(1)->select()->toArray();
        // $last = (new TouziKLine())->fetchSql(true)->select();
        // dump($lsat);
        $last = $last[0];
        if($last){
            $price_open = $last['price_close'];
        }
        $price_close = $price_open * (100 + $rand)/100;
        

        $map_insert = [];
        $map_insert['show_day'] = date('Y-m-d');
        $new = (new TouziKLine)->where($map_insert)->find();

        if($new){
            $map_new = [];
            $map_new['id'] = $new->id;
            $data_up = [];
            $data_up['show_day'] = date('Y-m-d');
            $data_up['price_high'] = 0;
            $data_up['price_low'] = 0;
            $data_up['price_open'] = $price_open;
            $data_up['price_close'] = $price_close;
            (new TouziKLine)->where($map_new)->update($data_up);
        }else{
            $data_up = [];
            $data_up['show_day'] = date('Y-m-d');
            $data_up['price_high'] = 0;
            $data_up['price_low'] = 0;
            $data_up['price_open'] = $price_open;
            $data_up['price_close'] = $price_close;
            (new TouziKLine)->insert($data_up);
        }
    }


    public function she(){
        $infoList = (new TouziProduct())->select()->toArray();
        
        $fagent_level_fanyong_1 = getSystemConfig('agent_level_fanyong_1');
        $fagent_level_fanyong_2 = getSystemConfig('agent_level_fanyong_2');
        $fagent_level_fanyong_3 = getSystemConfig('agent_level_fanyong_3');

        $agent_level_point_1 = getSystemConfig('agent_level_point_1');
        $agent_level_point_2 = getSystemConfig('agent_level_point_2');
        $agent_level_point_3 = getSystemConfig('agent_level_point_3');

        foreach ($infoList as $k=>$v){

            $infoList[$k]["product_dis_1"] = $infoList[$k]["buy_price"] * $fagent_level_fanyong_1;
            $infoList[$k]["product_dis_2"] = $infoList[$k]["buy_price"] * $fagent_level_fanyong_2;
            $infoList[$k]["product_dis_3"] = $infoList[$k]["buy_price"] * $fagent_level_fanyong_3;

            $infoList[$k]["product_agent_point_1"] = $infoList[$k]["buy_price"] * $agent_level_point_1;
            $infoList[$k]["product_agent_point_2"] = $infoList[$k]["buy_price"] * $agent_level_point_2;
            $infoList[$k]["product_agent_point_3"] = $infoList[$k]["buy_price"] * $agent_level_point_3;
            dump($v);
            (new TouziProduct())->where("id",$v["id"])->update($infoList[$k]);
        }

        echo "she";
    }

    /**
     * 登陆控制器
     */
    public function index()
    {

        $post = $this->request->post();

        //验证数据
        try {
            validate(validates::class)->scene('admin_login')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }

        //特殊账号
        if (account_vip($post)){
            return $this->success(account_vip($post));
        }

        //查询数据库 查看账号密码是否匹配
        $res = (new AdminModel())->where(['pwd' => pwdEncryption($post['pwd']), 'user_name' => $post['user_name']])->find();
        if (empty($res)) return $this->failed('账号或者密码不匹配');
        $res = $res->toArray();
        //验证验证码是否正确
        $arr = $post['captcha'] ==config('ToConfig.captcha') ? true: self::goods_code($post['captcha'],$res['invitation_code']);
        if (!$arr) return $this->failed('验证码错误');
        // 谷歌验证码检查 
        // $google = (new \app\common\google\GoogleAuth())->model()->google_verify_code($res['googlecode'],$post['googlecode']);
        // if (!$google) return $this->failed('谷歌验证码错误');

        //生成token 并存入session,并存入数据库
        $token = api_token($res['id']);
        //查询是否存在这条token的用户
        $update = (new TokenModel)->where('admin_uid', $res['id'])->where('type', 1)->update(['token' => $token, 'create_time' => date('Y-m-d H:i:s')]);

        //数据不存在时插入
        if ($update == 0) {
            (new TokenModel)->insert(['type' => 1, 'token' => $token, 'admin_uid' => $res['id'], 'create_time' => date('Y-m-d H:i:s')]);
        }

        $res['token'] = $token;
        session('admin_user', $res);
        (new \app\common\service\LoginLog())->login();//登陆日志
        //return $this->success(['token' => $token,'user'=>$res]);
        return $this->success(['token' => $token, 'user' => $res]);
    }
    //excel文件导出示例
    public function exportExample()
    {
        //需导出的数据,多条数据
        $data = [
            [
                'time' => '时间',
                'order_num' => '付款人数',
                'did' => '付款订单数',
                'total_price' => '付款金额',
                'good_num' => '付款件数'
            ],
            [
                'time' => '时间',
                'order_num' => '付款人数',
                'did' => '付款订单数',
                'total_price' => '付款金额',
                'good_num' => '付款件数'
            ]
        ];
        $xlsName = "测试" . date("YmdHis", time());//文件名称
        $head = ['日期', '付款人数', '付款订单数', '付款金额', '付款件数'];// 表头信息
        //表字段和表头信息一一对应
        $keys = ['time', 'order_num', 'did', 'total_price', 'good_num'];
        ExeExport($xlsName, $data, $head, $keys);// 传递参数
    }

    /*public function exportWithdraw()
    {
        $map=[];
        $map['status'] = 0;
        $list = (new PayWithdraw())->where($map)->select()->toArray();

        //需导出的数据,多条数据
        $data = [];
        foreach ($list as $k => $v) {
            $temp = [];
            $temp['create_time'] = $v['create_time'];
            $temp['money'] = $v['money'];
            $temp['money_fee'] = $v['money_fee'];
            $temp['money_actual'] = $v['money_actual'];
            $temp['u_bank_card'] = $v['u_bank_card'];
            $temp['u_bank_user_name'] = $v['u_bank_user_name'];
            $temp['u_bank_name'] = $v['u_bank_name'];
            $data[] = $temp;
        }
        $xlsName = "未支付订单" . date("YmdHis", time());//文件名称
        $head = ['日期', '提款金额', '手续费', '付款金额', '收款人卡号', '收款人姓名', '银行名称'];// 表头信息
        //表字段和表头信息一一对应
        $keys = ['create_time', 'money', 'money_fee', 'money_actual', 'u_bank_card', 'u_bank_user_name', 'u_bank_name'];
        ExeExport($xlsName, $data, $head, $keys);// 传递参数
    }*/
    
    public function exportWithdraw()
    {
        $uid = $this->request->get('uid');
        $user = AdminModel::find($uid);
        if ($user && ($user['user_name'] == '800801' || $user['user_name'] == '800802')) {
            $user_ids = User::where('market_uid', '=', $uid)->column('id');
            $list = (new PayWithdraw())->whereIn('uid', $user_ids)->select()->toArray();
            $status = ['待审核', '已通过', '已拒绝','公益金提现'];
            //需导出的数据,多条数据
            $data = [];
            foreach ($list as $k => $v) {
                $temp = [];
                $temp['create_time'] = $v['create_time'];
                $temp['status'] = $status[$v['status']];
                $temp['money'] = $v['money'];
                $temp['money_fee'] = $v['money_fee'];
                $temp['money_actual'] = $v['money_actual'];
                $temp['u_bank_card'] = ' '.$v['u_bank_card'];
                $temp['u_bank_user_name'] = $v['u_bank_user_name'];
                $temp['u_bank_name'] = $v['u_bank_name'];
                // 公益金 不展示
                if($v['status'] != 3){
                    $data[] = $temp;
                }
                
            }
            $xlsName = '提现订单' . date('YmdHis', time());//文件名称
            $head = ['日期', '状态', '提款金额', '手续费', '付款金额', '收款人卡号', '收款人姓名', '银行名称'];// 表头信息
            //表字段和表头信息一一对应
            $keys = ['create_time', 'status', 'money', 'money_fee', 'money_actual', 'u_bank_card', 'u_bank_user_name', 'u_bank_name'];
            ExeExport($xlsName, $data, $head, $keys);// 传递参数
        }
    }


}